package com.service.screenWeb.controller;

import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;

import com.service.screenWeb.dto.PosterDto;
import com.service.screenWeb.service.FileService;
import com.service.screenWeb.service.VFilePhysicsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fileUpload")
@Api(value = "上传海报/轮播图",produces = "上传海报/轮播图")
public class FileUploadController {
    @Resource
    private FileService service;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation(value = " 轮播图上传")
    @RequestMapping(value = "/uploadSlide", method = RequestMethod.POST,headers = "Content-Type=multipart/form-data")
    @ResponseBody
    public Result uploadSlide(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                    @RequestParam(value = "file",required = false) MultipartFile[] files,
                                    @RequestParam("assemblyId")@ApiParam("组件id") String assemblyId,
                                    @RequestParam("programmeId")@ApiParam("方案id") String programmeId,
                                    @RequestParam("interval")@ApiParam("轮播间隔")String interval) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (!redisUtil.exist(cloudAuthToken + userId)) {
                return Result.failure(10001, "未登录或者用户不存在，请重新登录");
            }
            if(files.length>=3 && files.length<=5)
            {
                if(service.findFileByAssemblyId(assemblyId,programmeId) != 0)
                {
                    service.deleteFileByAssemblyId(assemblyId,programmeId);
                }
                for (MultipartFile file : files) {
                    service.uploadSlide(file,assemblyId,programmeId,interval);
                }
            }else if(files.length==0)
            {
                service.updateInterval(assemblyId,programmeId,interval);
            }else {
                return Result.failure(10001,"上传海报数量有误,请检查确认");
            }
            return Result.ok();
        }
        return Result.ok();
    }

    @ApiOperation(value = " 海报上传")
    @RequestMapping(value = "/uploadPoster", method = RequestMethod.POST,headers = "Content-Type=multipart/form-data")
    @ResponseBody
    public Result uploadPoster(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                    @RequestParam("file") MultipartFile file,
                                    @RequestParam("assemblyId")@ApiParam("组件id") String assemblyId,
                                    @RequestParam("programmeId")@ApiParam("方案id") String programmeId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (!redisUtil.exist(cloudAuthToken + userId)) {
                return Result.failure(10001, "未登录或者用户不存在，请重新登录");
            }
            if(service.findFileByAssemblyId(assemblyId,programmeId) != 0)
            {
                service.deleteFileByAssemblyId(assemblyId,programmeId);
            }
            service.uploadPoster(file,assemblyId,programmeId);
            return Result.ok();
        }
        return Result.ok();
    }

    @ApiOperation(value = " 查询海报地址")
    @RequestMapping(value = "/getPosterDic", method = RequestMethod.GET)
    @ResponseBody
    public Result getPosterDic(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestParam("assemblyId")@ApiParam("组件id") String assemblyId,
                               @RequestParam("programmeId") String programmeId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (!redisUtil.exist(cloudAuthToken + userId)) {
                return Result.failure(10001, "未登录或者用户不存在，请重新登录");
            }
            PosterDto poster = service.getPosterDic(assemblyId,programmeId);
            Map<String,String> map=new HashMap<>();
            map.put("filePath",poster.getFilePath());
            return Result.ok().setData(map);
        }
        return Result.ok();
    }

    @ApiOperation(value = " 查询轮播图地址、间隔")
    @RequestMapping(value = "/getSlidePath", method = RequestMethod.GET)
    @ResponseBody
    public Result getSlidePath(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestParam("assemblyId")@ApiParam("组件id") String assemblyId,
                               @RequestParam("programmeId") String programmeId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (!redisUtil.exist(cloudAuthToken + userId)) {
                return Result.failure(10001, "未登录或者用户不存在，请重新登录");
            }
            List<PosterDto> slides = service.getSlideInfo(assemblyId,programmeId);

            List<Object> list=new ArrayList<>();
            List<Object> list2=new ArrayList<>();

            Map<String,Object> map1=new HashMap<>();
            map1.put("interval",Integer.parseInt(slides.get(0).getInterval()));
            for (PosterDto slide:slides)
            {
                list2.add(slide.getFilePath());
            }
            map1.put("imageList",list2);
            list.add(map1);
            return Result.ok().setData(map1);
        }
        return Result.ok();
    }
}
